/**
 * @file timer.c
 * @brief COSC361 - MCU Jukebox Project\n
 * Group: Hamish Cumming, Brian Thorne\n
 * Latest source can be obtained using SVN from google's code repository with the following command:\n
 * svn checkout http://mega8-hob.googlecode.com/svn/trunk/ jukebox-hamish-brian\n
 * Description: Source file for the timer module\n
 * Date: 25/5/2008
 */
 
 #include "Board.h"
 #include "timer.h"
 
/**
 * @brief Initialise timer0
 */ 
 void timer0_init(void)
 {
    AT91PS_TCB pTCB = AT91C_BASE_TCB;
    pTCB->TCB_BCR = 0;
    pTCB->TCB_BMR = 0x15;
    AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, ((unsigned int) 1 << AT91C_ID_TC0));
    //timer0 reg
    AT91PS_TC pTC  = AT91C_BASE_TC0;
    pTC->TC_CCR = 0x05;
    pTC->TC_CMR = 0x8003;
} 
 
/**
 * @brief Reads timer0 value
 * @return The current value of timer0
 */
 inline unsigned int timer0_getCurrentValue(void)
{
    return AT91C_BASE_TC0->TC_CV;
}

/**
 * @brief Initialise timer1
 */ 
 void timer1_init(void)
 {
    //need to set these    
    AT91PS_TCB pTCB = AT91C_BASE_TCB;
    pTCB->TCB_BCR = 0;
    pTCB->TCB_BMR = 0x15;
    AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, ((unsigned int) 1 << AT91C_ID_TC1));
    //timer1 reg
    AT91PS_TC pTC  = AT91C_BASE_TC1;
    pTC->TC_CCR = 0x05;
    pTC->TC_CMR = 0x8004;
} 
 
/**
 * @brief Reads timer1 value
 * @return The current value of timer1
 */
inline unsigned int timer1_getCurrentValue(void)
{
    return AT91C_BASE_TC1->TC_CV;
}

/**
 * @brief Determines if timer1 has overflowed
 * @return The Overflow status of timer1
 */
inline int timer1_overflow(void)
{
    AT91PS_TC pTC  = AT91C_BASE_TC1;
    return  pTC->TC_SR & 0x01;
}

/**
 * @brief Reset timer0
 */
inline void timer0_reset(void)
{
    AT91C_BASE_TC0->TC_CCR |= 0x04;
}

/**
 * @brief Reset timer1
 */
inline void timer1_reset(void)
{
    AT91C_BASE_TC1->TC_CCR |= 0x04;
}
